//
//  ZCeatweeklyVC.m
//  zctx
//
//  Created by apple on 16/1/20.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCeatweeklyVC.h"
#import "ZCeatweeklyCell.h"
#import "ZCinformationVC.h"
@interface ZCeatweeklyVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,assign)NSInteger offset;
@end

@implementation ZCeatweeklyVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCeatweeklyVC"];
    [MTA trackPageViewBegin:@"ZCeatweeklyVC"];
      self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCeatweeklyVC"];
    [MTA trackPageViewEnd:@"ZCeatweeklyVC"];
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"食事周刊";
    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];
    [self initnavegation];
    _offset = 0;
    self.dataArray = [[NSArray alloc]init];
    [self initTableView];
    [self requestData];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCeatweeklyCell" bundle:nil] forCellReuseIdentifier:@"eatweekCell"];
    self.tableView.mj_footer = [MJDIYFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

#pragma mark 加载更多
-(void)loadMoreData{
    //上拉加载
    _offset = _offset+1;
    [self requestData];
    
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)buttonClick{
    NSLog(@"sdf");
}

#pragma mark 我的数据
-(void)requestData{
    
    if (_offset==0) {
        [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    }
    [NetworkManager requestIndexGetweeklylistpagecount:[NSString stringWithFormat:@"%ld",(long)_offset] success:^(NSDictionary *dictResponse) {
    [self.tableView.mj_footer endRefreshing];
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){

        NSArray *sarray = (NSArray *)[dictResponse objectForKey:@"content"];
        if (sarray.count>0) {
            self.dataArray = [sarray copy];
            [self.tableView reloadData];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }
        
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];
}
-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorColor = RGB(236, 237, 238);
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}
#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"eatweekCell";
    ZCeatweeklyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [(ZCeatweeklyCell *)[ZCeatweeklyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section];
    cell.contentDict = dict;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.001;
    }else{
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.3];
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.section];
    ZCinformationVC *vc = [[ZCinformationVC alloc] init];
    vc.strURL = [NSString stringWithFormat:@"%@",[dict objectForKey:@"url"]];
    vc.title = [NSString stringWithFormat:@"食事周刊"];
    vc.urlID = [NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]];
    NSLog(@"vc.urlid = %@",vc.urlID);
    vc.isweekly = @"1";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}

-(void)unselectCell:(id)sender{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
